<template>
  <div class="xtx-infinite-loading" ref="target">
    <div class="loading" v-if="props.loading">
      <span class="img"></span>
      <span class="text">正在加载...</span>
    </div>
    <div class="none" v-if="props.finished">
      <span class="img"></span>
      <span class="text">{{ props.finishedText }}</span>
    </div>
  </div>
</template>
<script setup>
import { defineProps, defineEmits, ref } from 'vue'
import { useIntersectionObserver } from '@vueuse/core'
const props = defineProps({
  loading: {
    type: Boolean,
    default: false // true代表当前页数据正在加载中，false代表当前页数据已经加载完毕
  },
  finished: {
    type: Boolean,
    default: false // true代表所有的数据已经加载完毕了，没有更多数据了
  },
  finishedText: {
    type: String,
    default: '亲，没有更多了'
  }
})
const emit = defineEmits(['load'])
const target = ref(null)
useIntersectionObserver(
  target, // 要监听的dom
  ([{ isIntersecting }]) => {
    // console.log(isIntersecting)
    if (isIntersecting) { // isIntersecting代表已经进入到可视区
      if (props.loading === false && props.finished === false) {
        // 触发父组件中加载下一页数据的自定义事件
        emit('load')
      }
    }
  },
  {
    threshold: 0
  }
)
// 监听当前组件位置是否进入了可视区
</script>
<style lang="less" scoped>
.xtx-infinite-loading {
  .loading {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 200px;

    .img {
      width: 50px;
      height: 50px;
      background: url(../../assets/images/load.gif) no-repeat center / contain;
    }

    .text {
      color: #999;
      font-size: 16px;
    }
  }

  .none {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 200px;

    .img {
      width: 200px;
      height: 134px;
      background: url(../../assets/images/none.png) no-repeat center / contain;
    }

    .text {
      color: #999;
      font-size: 16px;
    }
  }
}
</style>